const { addGoodsCart, isCartExist, updateGoods, getUserGoods, delGoodsCart } = require('../model/goodscart')

// 添加购物车
const addGoods = (req, resp) => {
        const { _id, phone, car_size, goods_count } = req.body
        let count = goods_count * 1
        isCartExist({ _id, phone }).then(respmodel => {
            const { isExist, goodinfo } = respmodel;
            if (isExist) {
                updateGoods({ _id, phone }, { _id, phone, car_size, goods_count: (goodinfo.goods_count + count) }).then(list => {
                    resp.json({
                        code: 200,
                        body: {
                            msg: '修改成功',
                            list
                        }
                    })
                }).catch(err => {
                    resp.json({
                        code: 400,
                        body: {
                            msg: '修改失败',
                            err
                        }
                    })
                })
            } else {
                addGoodsCart({ _id, phone, car_size, goods_count }).then(goodone => {
                    resp.json({
                        code: 200,
                        body: {
                            msg: '添加成功',
                            goodone
                        }
                    })
                }).catch(err => {
                    resp.json({
                        code: 401,
                        body: {
                            msg: '网络错误，请重试，添加失败',
                            err
                        }
                    })
                })
            }
        }).catch(err => {
            resp.json({
                code: 400,
                body: {
                    msg: '网络错误，请重试，连接查询失败',
                    err
                }
            })
        })
    }
    // 查询购物车
const getGoods = (req, resp) => {
    const { phone } = req.body
    getUserGoods({ phone }).then(respmodel => {
        const docs = respmodel.docs
        resp.json({
            code: 200,
            body: {
                msg: '查询成功',
                list: docs
            }
        })
    }).catch(err => {
        resp.json({
            code: 400,
            body: {
                msg: '网络错误，请重试，连接查询失败',
                err
            }
        })
    })
}
const delGoods = (req, resp) => {
    const { _id, phone } = req.body
    delGoodsCart({ _id, phone }).then(respmodel => {
        const docs = respmodel.docs
        resp.json({
            code: 200,
            body: {
                msg: '删除购物车成功',
                list: docs
            }
        })
    }).catch(err => {
        resp.json({
            code: 400,
            body: {
                msg: '网络错误，请重试，删除购物车失败',
                err
            }
        })
    })
}
const updateCar = (req, resp) => {
    const { _id, phone, goods_count } = req.body
    updateGoods({ _id, phone }, { goods_count }).then(list => {
        resp.json({
            code: 200,
            body: {
                msg: '修改成功',
                list
            }
        })
    }).catch(err => {
        resp.json({
            code: 400,
            body: {
                msg: '修改失败',
                err
            }
        })
    })
}
module.exports = {
    addGoods,
    getGoods,
    delGoods,
    updateCar
}